<template>
<div class="platform-admin views-container">
  <el-dialog @close="saveDialogForm" :visible.sync="dialogTableVisible" custom-class="reset-pwd wlm-form-dialog dialog-table">
    <div class="wlm-form" v-if="dialogTableVisible">
      <div class="wlm-form-header wlm-dialog-fixheader">
        <span class="wlm-dialog-inlenr">{{`${!!formFormatData.api['editForm']['params']['user_id']?'编辑':'添加'}平台管理员`}}</span>
      </div>
      <el-scrollbar wrap-class="scrollbar-wrapper">
        <div class="wlm-form-content">
          <div class="wlm-dialog-main">
            <div class="wlm-dialog-content">
              <el-form ref="user" :model="formFormatData.formData" :rules="formFormatData.rules" size="small" label-width="120px" class="retail-form" label-position="right">
                <el-form-item label="用户">
                <span v-if="!!formFormatData.api['editForm']['params']['user_id']">{{formFormatData.formData.nickName}}</span>

                <chooseUser v-else :config="initChooseUser" ref="chooseUser" :changeEvt="userChange">
                  <!-- <template slot="el-table-column">
                      <el-table-column label="所属门店" >
                            <template slot-scope="scope">
                                <span>{{ scope.row.bus_name || '-' }}</span>
                            </template>
                      </el-table-column>
                  </template> -->
                </chooseUser>
                </el-form-item>
                <el-form-item label="姓名">
                  <el-input v-model.trim="formFormatData.formData.name">
                  </el-input>
                </el-form-item>
                <el-form-item label="联系方式">
                  <el-input v-model.trim="formFormatData.formData.mobile">
                  </el-input>
                </el-form-item>
              </el-form>
            </div>
          </div>
        </div>
      </el-scrollbar>
    </div>
    <footer class="flex-row flex-align-c flex-justify-c wlm-dialog-fixfooter">
      <el-button size="small" @click="saveDialogSubmit" type="primary">保存</el-button>
    </footer>
  </el-dialog>
  <div class="wlm-table">
    <div class="wlm-table-hearder-btn">
      <el-button size="small" type="primary" @click="dialogTableVisible = true;initChooseUser.closable = true;">添加平台管理员</el-button>
    </div>
    <div class="wlm-table-header">
      <el-form :model="tableFormatData.userTable.files" size="small" label-width="110px" class="retail-form" label-position="right">
        <el-form-item label="平台管理员姓名：" class="search">
          <el-input v-model="tableFormatData.userTable.files.name" placeholder="请输入平台管理员姓名" style="width:180px;"></el-input>
        </el-form-item>
        <el-form-item label="" class="filter-btns">
          <el-button v-waves type="primary" @click="filesSerch">筛选</el-button>
          <el-button type="text" @click="filesEmpty">清空筛选条件</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="wlm-table-content">
      <el-table :ref="tableFormatData.userTable.key" :data="tableFormatData.userTable.tableData" @selection-change="handleSelectionChange" style="width: 100%">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column prop="date" label="用户" >
          <template slot-scope="scope">
            <img class="logo" :src="scope.row.avatarUrl">
            <span> {{scope.row.nickName}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="date" label="姓名" >
          <template slot-scope="scope">
              <span>{{scope.row.name}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="联系方式" >
          <template slot-scope="scope">
            <span>{{scope.row.mobile}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" >
          <template slot-scope="scope">
            <div class="operation-group">
              <el-button class="wlm-text" type="text" @click="initChooseUser.closable = false;formFormatDialogEditEvt({id:scope.row.user_id,toggle:'dialogTableVisible'})">编辑</el-button>
              <div class="btn-line"></div>
              <el-button class="wlm-text" type="text" @click="delTableItem(scope.row.user_id)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-content flex-row flex-justify-b flex-align-c">
        <el-row class="pagination-btns">
          <el-checkbox class="check-all"  true-label="1" false-label="0" v-model="tableFormatData.userTable.files.checkall" @change="toggleSelection">全部</el-checkbox>
          <el-button class="right-8" :disabled="isGroup" size="mini" @click="delTableList">删除</el-button>
        </el-row>
        <el-pagination :disabled="!hasTableData" @size-change="listPageChange" @current-change="listPageChange" :current-page.sync="tableFormatData.userTable.pagination.page" :page-sizes="tableFormatData.userTable.pagination.pagesizes" :page-size.sync="tableFormatData.userTable.pagination.list_rows" layout="total, sizes, prev, pager, next, jumper" :total="hasTableData?tableFormatData.userTable.pagination.total : 0">
        </el-pagination>
      </div>
    </div>
  </div>
</div>
</template>

<script>
import {
  storeAdminAddEdit,
  storeAdminDetail,
  storeAdminIndex,
  storeAdminDelete
} from '@/api/platform'
import waves from '@/directive/waves' // Waves directive
import ChooseUser from '@/components/ChooseUser/index'
import mixins from '@/mixins/mixins'
export default {
  mixins: [mixins.getters('Table'), mixins.getters('Form')],
  name: 'PlatformAdmin',
  components: {
    ChooseUser
  },
  directives: {
    waves
  },
  created() {
  },
  data() {
    return {
      initChooseUser: {
        closable: true,
        initList: []
      },
      dialogTableVisible: false,
      formFormatData: {
        key: 'user',
        model: {
          noCreateEdit: false,
          hasMessage: false,
          closeDialog: false,
          bindDialog: 'dialogTableVisible'
        },
        api: {
          editForm: {
            api: storeAdminDetail,
            params: {
              id: ''
            },
            redirect: 'user_id'
          },
          submitForm: storeAdminAddEdit
        },
        formData: {
          user_id: '',
          name: '',
          nickName: '',
          mobile: ''
        },
        rules: {
        }
      },
      tableFormatData: {
        current: 'userTable',
        userTable: {
          key: 'userTable',
          api: {
            getList: storeAdminIndex,
            delList: storeAdminDelete
          },
          tableData: [],
          files: {
            name: '',
            ids: [],
            checkall: '0',
            redirect: 'user_id',
            Recycle: '1'
          },
          pagination: {
            page: 1,
            list_rows: 10,
            pagesizes: [10, 20, 50],
            total: 0
          }
        }
      }
    }
  },
  methods: {
    saveDialogSubmit() {
      this.formFormatSubmit()
    },
    saveDialogForm() {
      this.initChooseUser.initList = []
      this.resetDialogFormData({ user_id: '', name: '', mobile: '', nickName: '' })
    },
    formFormatCallBack(data, type, response) {
      if (type === 'editForm') {
        const { user_id, nickName } = data
        this.initChooseUser.initList = [{ user_id: user_id, nickName: nickName }]
      } else if (type === 'submitForm') {
        console.log(response, 'formFormatCallBack')
        const { data: { code, msg } } = response
        const hasId = Object.keys(this.$route.query).includes(this.formFormatData.api['editForm']['redirect'] || 'id')
        const isEdit = this.formFormatData.api['editForm'] && this.formFormatData.api['editForm']['api'] && ((this.$route.query && hasId) || (this.formFormatData.api['editForm']['params'] && this.formFormatData.api['editForm']['params']['always']))
        if (code === 1) {
          this.$message.success(isEdit && hasId ? '编辑成功' : '保存成功')
          this.dialogTableVisible = false
          this.getTableList()
        } else {
          this.$message.error(`${msg}`)
        }
      }
    },
    userChange(userlist) {
      console.log(userlist, 'userlist')
      const [{
        user_id
      }] = [...userlist]
      this.formFormatData.formData.user_id = user_id
    }
  }
}
</script>

<style lang="scss" scoped>
.platform-admin {
  .logo {
    height: 32px;
    width: 32px;
    border-radius: 32px;
    vertical-align: middle;
  }
}
</style>
